package com.sc.nft.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.ExternalLinkIcon;
import com.sc.nft.entity.dto.ExternalLinkIconIconUpdateDTO;
import com.sc.nft.entity.dto.ExternalLinkIconPageAdminDTO;
import com.sc.nft.entity.vo.ExternalLinkIconPageAdminVO;
import com.sc.nft.mapper.ExternalLinkIconMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * 外链图标管理表(ExternalLinkIcon)表Dao层
 *
 * @author makejava
 * @since 2023-03-06 13:35:17
 */
@Slf4j
@Repository
public class ExternalLinkIconDao extends ServiceImpl<ExternalLinkIconMapper, ExternalLinkIcon> {

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }

    /**
     * 后台获取公用icon的外链列表
     *
     * @param externalLinkPageAdminDTO
     * @return
     */
    public Page<ExternalLinkIconPageAdminVO> pageAdmin(ExternalLinkIconPageAdminDTO externalLinkIconPageAdminDTO) {
        return baseMapper.pageAdmin(externalLinkIconPageAdminDTO.toMp(), externalLinkIconPageAdminDTO);
    }

    /**
     * 根据唯一标识ID修改数据
     *
     * @param externalLinkIconIconUpdateDTO
     * @return
     */
    public Boolean updateById(ExternalLinkIconIconUpdateDTO externalLinkIconIconUpdateDTO) {
        externalLinkIconIconUpdateDTO.setIconUrl(externalLinkIconIconUpdateDTO.getIconUrl().replaceAll(" ", ""));
        externalLinkIconIconUpdateDTO.setIconName(externalLinkIconIconUpdateDTO.getIconName().replaceAll(" ", ""));
        return SqlHelper.retBool(baseMapper.updateAdminById(externalLinkIconIconUpdateDTO));
    }

    /**
     * 获取是否有同名数据
     *
     * @param id
     * @param name
     * @return
     */
    public Boolean sameName(Long id, String name) {
        if (StrUtil.isEmpty(name)) {
            return Boolean.FALSE;
        }
        QueryWrapper<ExternalLinkIcon> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", id);
        queryWrapper.eq("icon_name", name.replaceAll(" ", ""));
        return SqlHelper.retBool(baseMapper.selectCount(queryWrapper));
    }

    /**
     * 获取是否有同名数据
     *
     * @param id
     * @param name
     * @return
     */
    public Boolean sameName(String name) {
        QueryWrapper<ExternalLinkIcon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("icon_name", name.replaceAll(" ", ""));
        return SqlHelper.retBool(baseMapper.selectCount(queryWrapper));
    }

    /**
     * 根据id判断当前数据是否存在
     *
     * @param id
     * @return
     */
    public Boolean selectByIdExist(Long id) {
        QueryWrapper<ExternalLinkIcon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return SqlHelper.retBool(baseMapper.selectCount(queryWrapper));
    }

}
